package com.site.user.controller;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import com.site.data.QueryResult;
import com.site.user.pojo.TbOperation;
import com.site.user.pojo.TbPrivilege;
import com.site.user.pojo.TbUser;

import com.site.user.service.ITbOperationService;
import com.site.user.service.ITbPrivilegeService;
import com.site.user.service.ITbPrivilegeTempService;
import com.site.user.service.ITbResourceService;

@Controller
@RequestMapping("/privilege")
public class TbPrivilegeController extends BaseController {
	@Resource
	private ITbPrivilegeService tbPrivilegeService;
	@Resource
	private ITbPrivilegeTempService tbPrivilegeTempService;
	@Resource
	private ITbResourceService tbResourceService;
	
	@Resource
	private ITbOperationService tbOperationService;
	
	
	/**
	 * 查询资源分配树数据
	 * @param request
	 * @param response
	 * @param out
	 */
	@RequestMapping(value = "/ser_getResTree", method = RequestMethod.POST)
	public void getResTree(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
		String msg="";
		String idResource = ServletRequestUtils.getStringParameter(request, "idResource", "");
		String id = ServletRequestUtils.getStringParameter(request, "id", "");
		
		TbUser user = GetLoginSession(request, response);
		if (null != user) {
			msg=tbResourceService.getResPrivilegeTree(user.getIdUser(),id,idResource);	
			
		} else {
			msg = "-1";
		}
		out.print(msg);
	}

	/**
	 * 查询数据
	 * @param request
	 * @param response
	 * @param out
	 */
	@RequestMapping(value = "/ser_getOperations", method = RequestMethod.GET)
	public void getOperations(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
		
		QueryResult result=tbOperationService.queryData(null,null);
		out.print(result.getResult("json"));
	}
	@RequestMapping(value = "/ser_getSystems", method = RequestMethod.GET)
	public void getSystems(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
		String msg="";
		TbUser user = GetLoginSession(request, response);
		if (null != user) {
			msg=tbResourceService.getSystems(user.getIdUser());			
			
		} else {
			msg = "-1";
		}
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");
		
		out.print(msg);
	}
	/**
	 * 载入数据
	 * @param request
	 * @param response
	 * @param out
	 */

	@RequestMapping(value = "/ser_createPrivilegeTemp", method = RequestMethod.POST)
	public void createPrivilegeTemp(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
		String idrole=ServletRequestUtils.getStringParameter(request, "idRole", "");
		String idsystem=ServletRequestUtils.getStringParameter(request, "idSystem", "");
		String idOperation=ServletRequestUtils.getStringParameter(request, "idOperation", "");
		String idResource=ServletRequestUtils.getStringParameter(request, "idResource", "");
		String roleid=tbPrivilegeTempService.createPrivilegeTemp(idrole,idsystem,idOperation,idResource);	
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");
		out.print(roleid);
	}

	/**
	 * 加载数据
	 * @param request
	 * @param response
	 * @param out
	 */
	@RequestMapping(value = "/ser_LoadPrivGrid", method = RequestMethod.GET)
	public void loadPrivGrid(HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
		
		String idrole=ServletRequestUtils.getStringParameter(request, "idRole", "");
		String type=ServletRequestUtils.getStringParameter(request, "type", "");
		String xmlgrid=tbPrivilegeService.loadPrivGrid(idrole,type);
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/xml;charset=utf-8");
		out.print(xmlgrid);
	}

	/**
	 * 保存权限到正式库
	 * @param request
	 * @param response
	 * @param out
	 */
	@RequestMapping(value = "/ser_savePrivilege", method = RequestMethod.POST)
	public void savePrivilege(HttpServletRequest request, HttpServletResponse response, PrintWriter out, TbPrivilege entity) {
		String idrole=ServletRequestUtils.getStringParameter(request, "idRole", "");		
		String xmlgrid=tbPrivilegeService.savePrivilege(idrole);
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");
		out.print(xmlgrid);	
	}
	/**
	 * 删除临时表数据
	 * @param request
	 * @param response
	 * @param out
	 */
	@RequestMapping(value = "/ser_deleteTempPrivilege", method = RequestMethod.POST)
	public void deleteTempPrivilege(HttpServletRequest request, HttpServletResponse response, PrintWriter out, TbPrivilege entity) {
		String idrole=ServletRequestUtils.getStringParameter(request, "idRole", "");
		
		String xmlgrid=tbPrivilegeTempService.deleteTempPrivilege(idrole);
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");
		out.print(xmlgrid);
		

	}
	/**
	 * 删除正式表数据
	 * @param request
	 * @param response
	 * @param out
	 */
	@RequestMapping(value = "/ser_deletePrivilege", method = RequestMethod.POST)
	public void deletePrivilege(HttpServletRequest request, HttpServletResponse response, PrintWriter out, TbPrivilege entity) {
		String idrole=ServletRequestUtils.getStringParameter(request, "idRole", "");
		
		String xmlgrid=tbPrivilegeService.deletePrivilege(idrole);
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setHeader("Content-Type", "text/html;charset=utf-8");
		out.print(xmlgrid);
		

	}
	
	
}
